import { Spin,Message,Modal} from 'iview';
//加载中
export function createSpin(){
    Spin.show({
        render: (h) => {
            return h('div', [
                h('Icon', {
                    'class': 'demo-spin-icon-load',
                    props: {
                        type: 'ios-loading',
                        size: 18
                    }
                }),
                h('div','正在加载中,请稍候……')
            ])
        }
    });
}
export function createSpin2(){
    Spin.show({
        render: (h) => {
            return h('div', [
                h('Icon', {
                    'class': 'demo-spin-icon-load',
                    props: {
                        type: 'ios-loading',
                        size: 18
                    }
                }),
                h('div','图片/视频正在读取中,请稍候……')
            ])
        }
    });
}
export function isRealNumber (val) {
    if (typeof val !== 'number' && typeof parseInt(val) !== 'number') {
        return false
    }
    if (!isNaN(val) && !isNaN(parseInt(val))) {
        return true
    } else {
        return false
    }
}
// 验证电话号码
export function isTelephone (phonenum) {
    if (!phonenum) {
        return false
    }
    var pattern = /^[1][3,4,5,7,8][0-9]{9}$/
    var pattern2 = /^0\d{2,3}-?\d{7,8}$/
    if (!pattern.test(phonenum) && !pattern2.test(/^0\d{2,3}-?\d{7,8}$/)) {
        return false
    } else {
        return true
    }
}

// 验证 身份 证
export function isCardNo (card) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (reg.test(card) === false) {
        return false
    } else {
        return true
    }
}
// 验证 电子 邮箱
export function isEmail (email) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
    return reg.test(email)
}
export function normalReg(){
    let REGPATTERN = {
        //特殊字符
        SPECIA_REG:'[^"\'<>%;)(&+}{#;=$]+',//'[^`~!@#$^&*=|{}\':;\',\\\\[\\\\].<>/?~@#￥……&*——|{}【】‘；：”“\'。，、？]+',
        //英文字母、数字、下划线
        ENG_NUM_UNDER:'^[a-zA-Z0-9_]*$',
        //英文字母,数字
        ENG_NUM:'^[a-zA-Z0-9]*$',
        CHN_ENG_NUM:'^[\u4E00-\u9FA5A-Za-z0-9]+$',
        //正常字符,除特殊字符以外的其他所有字符，包括其他语言
        NORMAL_REG:'^[^$]+$'//'^[\u4E00-\u9FA5A-Za-z0-9 ()（）*-_]+$'
    };
    return REGPATTERN;
}
// 身份证校验
export function isCardID(sId) {
    let iSum = 0;
    const info = '';
    if (!/^\d{17}(\d|x)$/i.test(sId)) {
    return '你输入的身份证长度或格式错误';
    }
    sId = sId.replace(/x$/i, 'a');
    if (this.aCity[parseInt(sId.substr(0, 2), 10)] == null) {
    return '你的身份证地区非法';
    }
    const sBirthday = sId.substr(6, 4) + '-' + Number(sId.substr(10, 2)) + '-' + Number(sId.substr(12, 2));
    const d = new Date(sBirthday.replace(/-/g, '/'));
    if (sBirthday !== (d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate())) {
    return '身份证上的出生日期非法';
    }
    for (let i = 17; i >= 0; i--) {
    iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
    }
    if (iSum % 11 !== 1) {
    return '你输入的身份证号非法';
    }
    return true;
}
export function loading(flag) {
    const html = `<div class="loading">
    <img src="/assets/images/loading.jpg">
    </div>`;
    if (flag) {
    $('.smart-container').append(html);
    } else {
    $('.loading').remove();
    }
}
// 手机号码的正确检查
export function checkMobile(phoneNum) {
    const reg = /^1[3|4|5|7|8][0-9]{9}$/; // 验证规则
    const flag = reg.test(phoneNum); // true
    return flag;
}
//table页面，每页展示的条数，n等于除tbody之外的其它所有高度之和；
export function pageSize(n){
    if ((document.body) && (document.body.clientHeight)){
        return Math.round((document.body.clientHeight - n)/50);
    }
    else if ((document.documentElement) && (document.documentElement.clientHeight)) {
        return Math.round((document.documentElement.clientHeight - n)/50);  //n=250
    }
}
// 能输入数字和小数点的判断
export function numberFixed(target) {
    const newH = target.replace(/[^\d\.]*/g, '');
    if (String(newH).split('.').length > 2) {
    target = target.substr(0, target.length - 1);
    }
    if (String(newH).substr(-1, 1) !== '.') {
    const tmp = String(parseFloat(newH).toFixed(1));
    const p = parseFloat(tmp);
    target = p;
    }
    return target;
}
//保留小数点位数，target是原数字，num是保存的位数
export function initNumber(target,num){
    if(String(target).indexOf('.')>-1){
        let arr=String(target).split('.');
        if(arr[1].length > num){
            arr[1]=arr[1].substr(0,num);
            target=Number(arr.join('.'));
        }
    }
    return target;
}
//获取当前时间并格式化 2018-12-18 16:56:31
export function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var mon1 = "";
    var strDt = "";
    var strH = "";
    var strMin = "";
    var strSed = "";
    if (month >= 1 && month <= 9) {
    mon1 = "0" + month;
    }else{
    mon1 = month+"";
    }
    if (strDate >= 0 && strDate <= 9) {
    strDt = "0" + strDate;
    }else{
    strDt = strDate+""
    }
    if(date.getHours() >=0 && date.getHours() <=9){
    strH = "0"+date.getHours();
    }else{
    strH = date.getHours()+"";
    }
    if(date.getMinutes() >=0 && date.getMinutes() <=9){
    strMin = "0"+date.getMinutes();
    }else{
    strMin = date.getMinutes()+"";
    }
    if(date.getSeconds() >=0 && date.getSeconds() <=9){
    strSed = "0"+date.getSeconds();
    }else{
    strSed = date.getSeconds()+"";
    }
    var currentdate = date.getFullYear() + seperator1 + mon1 + seperator1 + strDt
    + " " + strH + seperator2 + strMin
    + seperator2 + strSed;
    return currentdate;
}
//获取时间格式为 2018_12_10_12_00_00
export function getNowFormatDate_underline(date) {
    var seperator = "_";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var mon1 = "";
    var strDt = "";
    var strH = "";
    var strMin = "";
    var strSed = "";
    if (month >= 1 && month <= 9) {
    mon1 = "0" + month;
    }else{
    mon1 = month+"";
    }
    if (strDate >= 0 && strDate <= 9) {
    strDt = "0" + strDate;
    }else{
    strDt = strDate+""
    }
    if(date.getHours() >=0 && date.getHours() <=9){
    strH = "0"+date.getHours();
    }else{
    strH = date.getHours()+"";
    }
    if(date.getMinutes() >=0 && date.getMinutes() <=9){
    strMin = "0"+date.getMinutes();
    }else{
    strMin = date.getMinutes()+"";
    }
    if(date.getSeconds() >=0 && date.getSeconds() <=9){
    strSed = "0"+date.getSeconds();
    }else{
    strSed = date.getSeconds()+"";
    }
    var formateDate = date.getFullYear() + seperator + mon1 + seperator + strDt +
    seperator + strH + seperator + strMin
    + seperator + strSed;
    return formateDate;
}

//获取某个时间的前/后几秒/分钟/小时/天 ，flg=1 前 flg=0 后 mss:毫秒数
export function getDateFront(date,flg,mss){
    var thisDate = new Date(date);
    if(flg =="1"){
    return new Date(thisDate.getTime() - mss);
    }else{
    return new Date(thisDate.getTime() + mss);
    }
}
//转换时间 格式：2019-12-31 05:07:45
export function convertTime(date){
    let Month=date.getMonth()+1;
    let Day=date.getDate();
    let hour=date.getHours();
    let minute=date.getMinutes();
    let second=date.getSeconds();
    if(Month<10){
        Month=`0${Month}`;
    };
    if(Day<10){
        Day=`0${Day}`;
    }   
    if(hour<10){
        hour=`0${hour}`;
    }
    if(minute<10){
        minute = `0${minute}`;
    }
    if(second<10){
    second=`0${second}`;
    }
    date=`${date.getFullYear()}-${Month}-${Day} ${hour}:${minute}:${second}`;    
    return date;
}
    //比较两个日期的大小
    export function compareDate(date0,date1){
    let Year0=date0.getFullYear();
    let Month0=date0.getMonth()+1;
    let Day0=date0.getDate();
    let Year1=date1.getFullYear();
    let Month1=date1.getMonth()+1;
    let Day1=date1.getDate();
    if((Year0 > Year1) || (Year0==Year1&& Month0>Month1) || (Year0==Year1&& Month0==Month1&&Day0>Day1)){
        return false;
    } else {
        return true;
    }
}
//转换日期 格式： 2019-06-08
export function convertDate(date){
    let Month=date.getMonth()+1;
    let Day=date.getDate();
    if(Month<10){
        Month=`0${Month}`;
    };
    if(Day<10){
        Day=`0${Day}`;
    }   
    date=`${date.getFullYear()}-${Month}-${Day}`;    
    return date;
}
//获取两个日期之间的所有日期   2019-06-08
export function getBetweenDateStr (startDate, endDate) {
    const dateList = [];
    let Year0=`${new Date(startDate).getFullYear()}`;
    let Month0=`${new Date(startDate).getMonth()+1}`;
    let Day0=`${new Date(startDate).getDate()}`;
    if(Number(Month0)<10){
        Month0=`0${Month0}`;
    }
    if(Number(Day0)<10){
        Day0=`0${Day0}`;
    }
    dateList.push(`${Year0}-${Month0}-${Day0}`);
    while (true) {
        startDate.setDate(startDate.getDate() + 1)
        if (startDate.getTime() < endDate.getTime()) {
            let Year=`${new Date(startDate).getFullYear()}`;
            let Month=`${new Date(startDate).getMonth()+1}`;
            let Day=`${new Date(startDate).getDate()}`;
            if(Number(Month)<10){
                Month=`0${Month}`;
            }
            if(Number(Day)<10){
                Day=`0${Day}`;
            }
            dateList.push(`${Year}-${Month}-${Day}`);  
        } else {
            break;
        }
    }
    let Year1=`${new Date(endDate).getFullYear()}`;
    let Month1=`${new Date(endDate).getMonth()+1}`;
    let Day1=`${new Date(endDate).getDate()}`;
    if(Number(Month1)<10){
        Month1=`0${Month1}`;
    }
    if(Number(Day1)<10){
    Day1=`0${Day1}`;
    }
    dateList.push(`${Year1}-${Month1}-${Day1}`);
    //当只有2个日期，并且相等时，删除其中1个；
    if(dateList.length==2 && dateList[0]==dateList[1]){
        dateList.splice(1,1);
    }
    return dateList;
}
//获取本月共有多少周
export function getWeekinMonth(date){
    var date = new Date((new Date(date).setDate(1)) || (new Date(date)).setDate(1));
    var firstWeekDate = 1;// 默认第一周是本月1号 为了模拟本月1号是否为本月第1周的判断
    if (date.getDay() === 1) { // 判断1号是周一
      firstWeekDate = 1;
    } else if (date.getDay() === 0) { // 判断1号是周日
      firstWeekDate = 8 - 7 + 1;
    } else { // 判断1号是周二至周六之间
      firstWeekDate = 8 - date.getDay() + 1;
    }
    date.setMonth(date.getMonth()+1);
    date.setDate(0);
    var monthHasDays = date.getDate();// 本月天数
    monthHasDays = date.getDate() - firstWeekDate + 1;
    var hasWeek = Math.ceil(monthHasDays/7); // 计算本月有几周
    return hasWeek;
}
//验证是否数字
export function checkNumner(val){
    let reg = /^[0-9]*$/;
    const flag = reg.test(val); // true
    return flag;
}
//格式化时间格式为yyyyMMdd
export function convertDateYyyymmdd(date){
    let year = date.getFullYear();
    let month = (date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1);
    let day = date.getDate()<10?"0"+date.getDate():date.getDate();
    return year.toString()+month.toString()+day.toString();
}
//获取今天后几天/前几天
/**
 * flg:年1、月2、日3
 * nums:年数、月数、日数
 */
export function getDateAfterBefore(flg,nums){
    let ret = '';
    let date = new Date();
    switch (flg) {
        case 1:
            ret =  date.getFullYear()+nums+"-" + (date.getMonth()+1) + "-" + date.getDate();
            break;
        case 2:
            let year = date.getFullYear();
            let month = date.getMonth() +nums;
            if(month == -1){
                year = year -1;
                month = 12; 
            }else{
                month += 1;
            }
            ret =  year+"-" + month + "-" + date.getDate();
            break;
        case 3:
            ret = date.setTime(date.getTime()+nums*24*60*60*1000);
            break;
        default:
            break;
    }
    
    return new Date(ret);
}
//验证是否时间格式
export function checkDateTime(val){
    let reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
    const flag = reg.test(val); // true
    return flag;
}
//验证是否是价格格式
export function checkPrice(val){
    let reg = /^(-?\d+)(\.\d+)?$/;
    const flag = reg.test(val); // true
    return flag;
}
//联级树形结构把null改成【】
export function isCascader(val){
    if(val && val.length>0){
        val.forEach(data=>{
            let child=data.children;
            if(child && child.length >0){
                child.forEach(data2=>{
                    let child2=data2.children;
                    if(child2 && child2.length >0){
                        child2.forEach(data3=>{
                            let child3=data3.children;
                            if(child3 && child3.length > 0){} else {
                                data3.children=[];
                            }
                        })
                    } else {
                        data2.children=[];
                    }
                });
            } else {
                data.children=[];
            }
        });
    }
    console.log(val);
    return val;
}